/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

/**
 *
 * @author David
 */
import java.util.*;
import java.io.*;
import AirLine.*;
import Database.DBObject;
import java.sql.*;
import java.math.*;

public class RandomDataCreatorFeagheWang {

    private RandomDataCreator rdc;
    /*
    private Set<Airport> airportList;
    private Set<Flight> flightList;
    private ArrayList<Airplane> planeList;
    private ArrayList<Airport> forFlights;
    private int nrFlights, nrRows, nrColumns, nrPlanes;
    String[] alpha = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    //private String[] alpha;
     * */

    public static void main(String[] args) {
        new RandomDataCreatorFeagheWang();

    }

    public RandomDataCreatorFeagheWang() {

        /*
        airportList = new TreeSet<Airport>();
        forFlights = new ArrayList<Airport>();
        flightList = new TreeSet<Flight>();
        planeList = new ArrayList<Airplane>();
        nrFlights = 10;
        nrPlanes = 10;
        nrRows = 5;
        nrColumns = 5;
        getAirports();
        getPlanes();
        getFlights();
         */

        //System.out.println(Math.random());
        // addAirports();
        RandomDataCreator.initialize();


        if (true) {
            if (true) {
                addAirports();
                addAirline();
            }

           addPlanes();
            addSeats();
            addFlights();
        } else {
            removeStuff();
        }
    }

    private void removeStuff() {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
                        stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("DELETE FROM Bookedtrips");
            stm.execute();
            mm.commit();
            stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("DELETE FROM Seat");
            stm.execute();
            mm.commit();
            
            stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("DELETE FROM Flightinstance");
            stm.execute();
            mm.commit();
           
            stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("DELETE FROM Flight");
            stm.execute();
            mm.commit();
            stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("DELETE FROM Airplane");
            stm.execute();
            mm.commit();
            stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("DELETE FROM Airline");
            stm.execute();
            mm.commit();
             stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("DELETE FROM Airport");
            stm.execute();
            mm.commit();
            
            mm.closeConnections();

        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }

    private void addAirline() {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("INSERT INTO AIRLINE (NAME, Airline_id, OWN_DISSCOUNT) values(?,?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");

            try {
                System.out.println("test heeere");
                stm.setString(1, "The data people");
                stm.setString(2, "" + 8);
                stm.setInt(3, 15);
                stm.execute();
                mm.commit();
            } catch (SQLException e) {
                try {
                    mm.rollBack();
                    System.out.println("fel i första..." + e.getLocalizedMessage());
                } catch (Exception se) {
                    System.out.println("feeel");
                }
            }

        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }

    public void addAirports() {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("INSERT INTO AIRPORT (AIRPORT_ID,NAME) values(?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Airport a : RandomDataCreator.getAirports()) {
                try {
                    stm.setString(1, a.getAirportCode());
                    stm.setString(2, a.getAirportName());

                    stm.execute();
                    mm.commit();
                } catch (SQLException e) {
                    try {
                        mm.rollBack();
                        System.out.println("fel i första..." + e.getLocalizedMessage());
                    } catch (Exception se) {
                        System.out.println("feeel");
                    }
                }
            }
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }

    private void addPlanes() {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("INSERT INTO Airplane (AIRPLANE_ID, name, fk_airline_id) values(?,?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Airplane p : RandomDataCreator.getAirplanes()) {

                try {

                    stm.setString(1, p.getId());
                    stm.setString(2, RandomDataCreator.getRandomLetters(4));
                    stm.setString(3, "8");
                    stm.execute();
                    mm.commit();
                } catch (SQLException e) {
                    try {
                        mm.rollBack();
                        System.out.println("fel i addplanes..." + e.getLocalizedMessage());
                    } catch (Exception se) {
                        System.out.println("feeel");
                    }
                }

            }
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }

    private void addSeats() {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("INSERT INTO Seat (fk_airplane_id, seat_Row, seat_no, seat_Type, seat_id) values(?,?,?,?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Airplane p : rdc.getAirplanes()) {
                for (Seat s : p.getSeats()) {
                    try {

                        //System.out.println("planet vi försöker med: "+p.getId());
                        stm.setString(1, p.getId());

                        stm.setInt(2, s.getRow());
                        stm.setInt(3, s.getColumn());
                        stm.setInt(4, s.getType());
                        stm.setString(5, RandomDataCreator.getRandomLetters(6));

                        stm.execute();
                        mm.commit();
                    } catch (SQLException e) {
                        try {
                            mm.rollBack();
                            System.out.println("fel i addseats..." + e.getLocalizedMessage());
                        } catch (Exception se) {
                            System.out.println("feeel");
                        }
                    }
                }
            }
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }

    private void addFlights() {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        PreparedStatement stm2;
        PreparedStatement stm3;

        try {
            mm.connect();
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Flight f : rdc.getFlights()) {
                try {
                    stm = mm.getConnection(DBObject.ORACLE_013).prepareStatement("INSERT INTO Flight(fk_airport_id_departure, fk_airport_id_arrival, fk_airplane_id, fare, flight_number, fk_airline_id) values(?,?,?,?,?,?)");
                    stm2 = mm.getConnection(DBObject.ORACLE_013).prepareStatement("INSERT INTO Flightinstance(fk_flight_id, departure_time, arrival_time) values(?,?,?)");
            
                    stm.setString(5, f.getCode());
                    stm.setString(1, f.getDepartureAirport().getAirportCode());
                    stm.setString(2, f.getDestinationAirport().getAirportCode());
                    stm.setString(3, f.getPlaneId());
                    stm.setInt(4, f.getPrice());
                    stm.setString(6, "8");
                    stm.execute();
                    mm.commit();

                    for (int i = 0; i < 10; i++) {
                        stm3 = mm.getConnection(DBObject.ORACLE_013).prepareStatement("select max(flight_id) from flight");
                        ResultSet result = stm3.executeQuery();
                        result.next();
                        //System.out.println(result.getString("max(flight_id)"));
                        stm2.setString(1, result.getString("max(flight_id)"));
                        Timestamp[] timstamp = RandomDataCreator.getTimes();
                        stm2.setTimestamp(2, timstamp[0]);
                        stm2.setTimestamp(3, timstamp[1]);
                        stm2.execute();
                        mm.commit();
                        stm3.close();
                    }
                    stm2.close();
                    stm.close();
                    

                } catch (SQLException e) {
                    try {
                        mm.rollBack();
                        System.out.println("fel i addflights" + e.getLocalizedMessage());
                    } catch (Exception se) {
                        System.out.println("feeel");
                    }

                }
            }
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }

        }
    }
}
